<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\GoodsPrice;
use App\Models\Goods;
use App\Models\Manufacturer;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class GoodsPriceController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new GoodsPrice(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('goods_id','产品名称')->display(function($Id) {
                return Goods::find($Id)->goods_name;
            });
            $grid->column('manufacturer_name','厂家名称')->display(function() {
                $manufacturer_id = Goods::find($this->goods_id)->manufacturer_id;
                return Manufacturer::find($manufacturer_id)->manufacturer_name;
            });
            $grid->column('p_price');
            $grid->column('s_price');
            $grid->column('now_date');

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new GoodsPrice(), function (Show $show) {
            $show->field('id');
            $show->field('goods_id')->as(function($goods_id){
                return Goods::find($goods_id)->goods_name;
            });
            $show->field('p_price');
            $show->field('s_price');
            $show->field('now_date');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new GoodsPrice(), function (Form $form) {
            $form->display('id');
            $form->select('goods_id', '产品名称')->options(Goods::pluck('goods_name','id')->toArray());
            $form->number('p_price');
            $form->number('s_price');
            $form->date('now_date')->format('YYYY-MM-DD');
        });
    }
}
